Retrofit(九) 错误处理

您所在的位置:网站首页 返回码500 unexpected character Retrofit(九) 错误处理

Retrofit(九) 错误处理

2024-07-15 18:22| 来源: 网络整理| 查看: 265

通常服务端返回的JSON数据格式是这样的:

{ "code":0, "msg":"successfully", "result":{"name":"alan","ago":18,"gender":"male"} }

其中的code字段是业务逻辑的错误码。

code = 0, 表示成功, 非0代表错误code = 1000, 表示没有权限code = 1001, 表示尚未注册等等

 

那么该如何去组织解析呢?

把服务端返回的error code, 统一封装成YourAppGlobleException, 抛出去,让UI或业务层去处理错误。

我是这样定义的:

public class ResponseInfo implements Serializable { @SerializedName("code") private int code; @SerializedName("msg") private String message; @SerializedName("result") private T result; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getResult() { return result; } public void setResult(T result) { this.result = result; } }

在接口定义中可以这样写:

public interface IDeviceService { @POST("v1/info/pair") Call pair(@HeaderMap Map heads, @Body Device device); }

那如何知道成功了,即使错误了,又该怎么统一处理呢?

protected T get(Call call) throws YourAppGlobleGlobleException{ try { Response response = call.execute(); ResponseInfo responseInfo = response.body(); if (responseInfo != null && responseInfo.getCode() != 0) { Log4j.warn(this.getClass().getSimpleName() + ", response: " + responseInfo.getCode() + "message: " + responseInfo.getMessage()); throw new YourAppGlobleGlobleException(responseInfo.getCode(), responseInfo.getMessage()); } if (responseInfo == null) { Log4j.warn(this.getClass().getSimpleName() + "responseInfo == null, ErrorConnectFailed"); throw new YourAppGlobleGlobleException(ErrorConnectFailed, ""); } return responseInfo.getResult(); } catch (IOException e) { Log4j.warn(this.getClass().getSimpleName() + "ErrorConnectFailed" + e.getMessage()); throw new YourAppGlobleGlobleException(ErrorConnectFailed, ""); } } public Device pair(Device device) { Call response = mService.pair(DefaultHeader, device); Device targetDevice = null; try { targetDevice = get(response); } catch (YourAppGlobleException e) { e.printStackTrace(); } return targetDevice ; }

 

上面是同步情况的错误处理,异步怎么做呢?把上面的内容封装在一个自定义的回调里面。

public abstract class AsyncCallback implements Callback { public abstract void onFailure(YourAppGlobleException e); public abstract void onResponse(T result); @Override public void onResponse(Call call, Response response) { ResponseInfo responseInfo = (ResponseInfo) response.body(); if (responseInfo != null && responseInfo.getCode() != 0) { onFailure(new YourAppGlobleException(responseInfo.getCode(), responseInfo.getMessage())); return; } if (responseInfo == null) { onFailure(new YourAppGlobleException(ErrorConnectFailed, "")); return; } onResponse(responseInfo.getResult()); } @Override public void onFailure(Call call, Throwable t) { onFailure(new YourAppGlobleException(ErrorConnectFailed, t.getMessage())); } }

 

public void pairAsync(AsyncCallback callback, Device device) { Call call = mService.pair(DefaultHeader, device); call.enqueue(callback); }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3